﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.UI;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.DAL;
using System.Collections.Generic;
using Telerik.Web.UI;
using System.Web.UI.WebControls;

namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class ICM_Inventory_Stock_Taking : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        D_IcmMasResourceItem d_resource_item = new D_IcmMasResourceItem();
        M_IcmMasResourceItem m_resource_item = new M_IcmMasResourceItem();
        D_IcmInventoryStockTakingItem d_stock_taking_item = new D_IcmInventoryStockTakingItem();
        M_IcmInventoryStockTakingItem m_stock_taking_item = new M_IcmInventoryStockTakingItem();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateUI("Load");
                pStockTakingCode.Text = Request.QueryString["icm_stock_taking_code"].ToString();
                LoadGrid();
                grdStockTaking.DataBind();
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            UpdateUI("Insert");
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (chkValidate())
            {
                try
                {
                    m_stock_taking_item.icm_stock_taking_code = pStockTakingCode.Text;
                    m_stock_taking_item.icm_res_item_seq = Convert.ToInt32(cmbChooseInventoryItem.SelectedValue);
                    m_stock_taking_item.quanlity_amount_stock = cn.ConvertNullInt(pAmountStock.Text);
                    m_stock_taking_item.quantity_amount_real = cn.ConvertNullInt(txtStockReal.Text);
                    m_stock_taking_item.remark = txtRemark.Text;
                    m_stock_taking_item.record_status = "U";
                    if (hStatus.Text == "Add")
                    {
                        m_stock_taking_item.created_by = cn.UserID;
                        m_stock_taking_item.created_when = DateTime.Now;
                        d_stock_taking_item.Insert_StockTaking_Item(m_stock_taking_item);
                        RadAjaxManager1.Alert("เพิ่มข้อมูลเรียบร้อยแล้ว");
                    }
                    else if (hStatus.Text == "Edit")
                    {
                        m_stock_taking_item.updated_by = cn.UserID;
                        m_stock_taking_item.updated_when = DateTime.Now;
                        d_stock_taking_item.Update_StockTaking_Item(m_stock_taking_item);
                        RadAjaxManager1.Alert("แก้ไขข้อมูลเรียบร้อยแล้ว");
                    }
                    LoadGrid();
                    grdStockTaking.Rebind();
                    UpdateUI("Save");
                }
                catch (Exception ex)
                {
                    RadAjaxManager1.Alert("เกิดข้อผิดพลาดระหว่างบันทึกข้อมูล");
                    throw (ex);
                }
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Cancel");
            ClearObject();
        }
        protected void cmbChooseInventoryItem_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            M_IcmMasResourceItem info = d_resource_item.GetListFillBySeq(cn.ConvertNullInt(cmbChooseInventoryItem.SelectedValue));
            //pAmountStock.Text = info.sum_quantity.ToString();
        }
        protected void cmbChooseInventoryItem_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<M_IcmMasResourceItem> infoList = d_resource_item.GetListFillByName(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbChooseInventoryItem.Items.Add(new RadComboBoxItem(infoList[i].icm_res_item_name, infoList[i].icm_res_item_seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdCode = new TableCell();
                Label lblCode = new Label();
                lblCode.Text = infoList[i].icm_res_item_code;
                tdCode.Width = Unit.Pixel(200);
                tdCode.Controls.Add(lblCode);
                tr.Cells.Add(tdCode);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].icm_res_item_name;
                tdName.Width = Unit.Pixel(200);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdDescr = new TableCell();
                Label lblDescr = new Label();
                lblDescr.Text = infoList[i].icm_res_item_descr;
                tdDescr.Width = Unit.Pixel(200);
                tdDescr.Controls.Add(lblDescr);
                tr.Cells.Add(tdDescr);
                ((Table)cmbChooseInventoryItem.Items[j].FindControl("tableInventoryItem")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void grdStockTaking_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            LoadGrid();
        }
        protected void grdStockTaking_ItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DataEdit")
                {
                    M_IcmInventoryStockTakingItem info = d_stock_taking_item.GetListFillBySeq(grdStockTaking.Items[e.Item.ItemIndex]["icm_stock_taking_item_seq"].Text);
                    cmbChooseInventoryItem.SelectedValue = info.icm_res_item_seq.ToString();
                    cmbChooseInventoryItem.Text = info.resource_name.ToString();
                    pAmountStock.Text = info.quanlity_amount_stock.ToString();
                    txtStockReal.Text = info.quantity_amount_real.ToString();
                    txtRemark.Text = info.remark.ToString();
                    UpdateUI("Edit");
                }
                else if (e.CommandName == "DataDelete")
                {
                    UpdateUI("Delete");
                    M_IcmInventoryStockTakingItem info = d_stock_taking_item.GetListFillBySeq(grdStockTaking.Items[e.Item.ItemIndex]["icm_stock_taking_item_seq"].Text);
                    m_stock_taking_item.icm_stock_taking_item_seq = info.icm_stock_taking_item_seq;
                    m_stock_taking_item.record_status = "D";
                    m_stock_taking_item.updated_by = cn.UserID;
                    m_stock_taking_item.updated_when = DateTime.Now;
                    d_stock_taking_item.Delete_StockTaking_Item(m_stock_taking_item);
                    LoadGrid();
                    grdStockTaking.Rebind();
                    RadAjaxManager1.Alert("ลบข้อมูลเรียบร้อยแล้ว");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
               
            }
        }
        private void LoadGrid()
        {
            grdStockTaking.DataSource = d_stock_taking_item.GetListFillStockTakingCode(pStockTakingCode.Text);
        }
        //=================================== Function & Utility ==============================
        private void ClearObject()
        {
            pAmountStock.Text = "";
            txtStockReal.Text = "";
            cmbChooseInventoryItem.ClearSelection();
            cmbChooseInventoryItem.Text = "";
            txtRemark.Text = "";
        }
        private void EnableObject(bool pFlag)
        {
            txtStockReal.Enabled = pFlag;
            cmbChooseInventoryItem.Enabled = pFlag;
            txtRemark.Enabled = pFlag;
        }
        private bool chkValidate()
        {
            bool pFlag = true;
            if (cmbChooseInventoryItem.SelectedValue == "")
            {
                pFlag = false;
                cmbChooseInventoryItem.Focus();
                RadAjaxManager1.Alert("กรุณาเลือกประเภทพัสดุที่เปรียบเทียบการตรวจนับ");
            }
            else if (txtStockReal.Text == "")
            {
                pFlag = false;
                txtStockReal.Focus();
                RadAjaxManager1.Alert("กรุณากรอกจำนวนพัสดุที่มีอยู่จริง");
            }
            return pFlag;
        }
        private void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    ClearObject();
                    EnableObject(false);
                    btnAdd.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    break;
                case "Insert":
                    hStatus.Text = "Add";
                    ClearObject();
                    EnableObject(true);
                    btnAdd.Enabled = false;
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    break;
                case "Edit":
                    hStatus.Text = "Edit";
                    EnableObject(true);
                    btnAdd.Enabled = false;
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    break;
                case "Save":
                    hStatus.Text = "";
                    EnableObject(false);
                    btnAdd.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    break;
                case "Cancel":
                    hStatus.Text = "";
                    EnableObject(false);
                    ClearObject();
                    btnAdd.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    break;
                case "Delete":
                    EnableObject(false);
                    ClearObject();
                    btnAdd.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    break;
            }
        }


    }
}
